﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using MasApp.DbUtil;

namespace MasApp.Model
{
    /// <summary>
    ///login 的摘要说明
    /// </summary>
    public class login
    {
        public string table = "login";
        public Db db;
        public login()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }

        #region loginModel

        string username;

        string password;
        bool available = true;

        public bool Available
        {
            get { return available; }
            set { available = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public string Username
        {
            get { return username; }
            set { username = value; }
        }

        #endregion loginModel

        public bool checkAuth(string username, string password)
        {
            Db db = new Db();
            //db.select = login.table + ".username,"+ login.table + ".password";
            //db.from = login.table + " left join" + users.table + "ON " + login.table + ".username=" + users.table + ".username";
            //db.where = "" + login.table + ".username=@us AND " + login.table + ".available = @avai";

            db.select = "username,password";
            db.from = this.table;
            db.where = "" + this.table + ".username=@us AND " + this.table + ".available = @avai";


            db.Cmd.Parameters.Add("@us", SqlDbType.VarChar).Value = username;
            db.Cmd.Parameters.Add("@avai", SqlDbType.Bit).Value = 1;
            db.Cmd.Connection.Open();
            SqlDataReader dr = db.Cmd.ExecuteReader();
            int userid;
            if (dr.Read())
            {
                userid = dr.IsDBNull(0) ? 0 : dr.GetInt32(0);
                dr.Close();
                db.Cmd.Connection.Close();
                return userid;
            }
            else
            {
                dr.Close();
                db.Cmd.Connection.Close();
                return 0;
            }
        }
    }
}